package handle

import (
	"context"
	"net/http"

	"hertzx/pkg/consts"
	"hertzx/pkg/utils"
	"protocx/entity"
	"protocx/protobufx"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	hutils "github.com/cloudwego/hertz/pkg/common/utils"
)

func NewResp() Resp {
	return Resp{}
}

type Resp struct {
}

func (r Resp) InitRoute(server *server.Hertz) {
	p := server.Group("/resp")
	p.GET("/string", r.String())
	p.GET("/json", r.Json())
	p.GET("/jsonp", r.JSONP())
	p.GET("/xml", r.XML())
	p.GET("/binary", r.Binary())
	p.GET("/markdown", r.Markdown())
	p.GET("/html", r.HTML())
	p.GET("/protobuf", r.Protobuf())
}
func (r Resp) String() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.WriteString("string")
	}
}
func (r Resp) Json() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(http.StatusOK, entity.Resp{
			Code: 200,
			Msg:  "",
			Data: nil,
		})
	}
}
func (r Resp) JSONP() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.PureJSON(http.StatusOK, &protobufx.LoginRequest{
			Code: 879,
		})
	}
}
func (r Resp) XML() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.XML(http.StatusOK, entity.Resp{
			Code: 200,
			Msg:  "",
			Data: nil,
		})
	}
}
func (r Resp) Binary() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.Write([]byte("abcd"))
	}
}
func (r Resp) Markdown() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
	}
}
func (r Resp) TextIndentedJSON() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.IndentedJSON(http.StatusOK, &protobufx.LoginRequest{
			Code: 879,
		})
	}
}
func (r Resp) HTML() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.HTML(http.StatusOK, "index.html", hutils.H{
			"message": utils.BuildMsg(consts.PageErr),
			"token":   utils.BuildMsg("token"),
		})
	}
}
func (r Resp) Protobuf() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.ProtoBuf(http.StatusOK, &protobufx.LoginRequest{
			Code: 200,
			Msg:  "asa",
		})
	}
}
